package com.snake.mylibrary.loader;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.snake.mylibrary.runtime.RuntimeTools;

import java.util.ArrayList;
import java.util.List;

public abstract class ArrayListAdapter<T> extends BaseAdapter {
    public static final int EVENT_APPEND_DATA = 10000;
    public static final int EVENT_UPDATE_DATA = 10001;
	protected Context mContext;
    /**adapter的数据源*/
    protected ArrayList<T> mData;
    
    private Handler mUiThread = new Handler()
    {
    	public void dispatchMessage(android.os.Message msg) 
    	{
    		if(msg.what == EVENT_APPEND_DATA)
    		{
    			T[] d = (T[])msg.obj;
        		appendInternal(d);
    		}
    		else if(msg.what == EVENT_UPDATE_DATA)
    		{
    			T[] d = (T[])msg.obj;
        		updateInternal(d);
    		}
    	};
    };
    
    
    public ArrayListAdapter(Context context) {
        mContext = context;
    }
    /**添加数据*/
    public void appendData(T[] data) {
        if(RuntimeTools.RunInMainThread(mContext))
        {
        	appendInternal(data);
        }else
        {
        	mUiThread.sendMessage(mUiThread.obtainMessage(EVENT_APPEND_DATA, data));
        }
    }
    /**更新数据*/
    public void updateData(T[] data)
    {
    	if(RuntimeTools.RunInMainThread(mContext))
        {
        	updateInternal(data);
        }else
        {
        	mUiThread.sendMessage(mUiThread.obtainMessage(EVENT_UPDATE_DATA, data));
        }
    }
    /**更新数据*/
    private void updateInternal(T[] data)
    {
    	if (data != null) {
            if(mData == null)
            {
            	mData = new ArrayList<T>(20);
            }else
            {
            	mData.clear();
            }
            for(T t : data){
            	mData.add( t );
            }
            notifyDataSetChanged();
        } else {
        	if(mData != null)
        	{
        		mData.clear();
        	}
            notifyDataSetInvalidated();
        }
    }
    /**添加数据*/
    private void appendInternal(T[] data)
    {
    	if (data != null) {
            if(mData == null)
            {
            	mData = new ArrayList<T>(20);
            }
            for(T t : data){
            	mData.add( t );
            }
            notifyDataSetChanged();
        } else {
            //使原数据无效
            notifyDataSetInvalidated();
        }
    }
    
    public List<T> getData() {
        return mData;
    }

    @Override
    public int getCount() {
        if (mData != null) {
            return mData.size();
        } else {
            return 0;
        }
    }
    
    public void clearAllData()
    {
    	if(mData!=null)
    	{
    		mData.clear();
    		notifyDataSetChanged();
    	}
    }

    @Override
    public Object getItem(int position) {
        if (mData != null) {
            return mData.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        if (mData != null) {
            return position;
        } else {
            return 0;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position < 0 || position >= mData.size()) {
            throw new ArrayIndexOutOfBoundsException();
        }
        T data = mData.get(position);
        View v;
        if (convertView == null) {
            v = onCreateView(mContext, parent);
        } else {
            v = convertView;
        }
        onBindData(v, position, data);
        return v;
    }

    public abstract View onCreateView(Context context, ViewGroup parent);

    public abstract void onBindData(View view, int position, T data);

}
